#include "intrins.h"
#include "STC8G.h"
#include "User.h"
#include "header.h"
#include "Key.h"
#include "eeprom.h"


#define	Timer_CS_L		0x00
#define	Timer_CS_H		0x4C	// 定时常数，11.0592MHz,定时50mS

#define	Timer_10ms_L		0x00
#define	Timer_10ms_H		0xDC	// 定时常数，11.0592MHz,定时10mS

#define	PWM0_CS_L		0xFE	// 7.35kHz(T0工作在12T下)
#define	PWM0_CS_H		0xFF	

unsigned char switch_1;
unsigned KeyNum;
unsigned char LastSelectorIll;
unsigned char LastSelectorAim;
unsigned char LastKey1;
unsigned char LastKey2;
unsigned char LastKey3;
unsigned char CurrentSelectorIll;
unsigned char CurrentSelectorAim;
unsigned char State_LightingLatched; // 照明常亮状态
unsigned char State_GreenLatched; // 绿光常亮状态
unsigned char State_GreenAim;   //开关
unsigned char State_IRAim;  // 功率档位
unsigned char State_IRILL;  // 功率档位
unsigned char State_LED;    //0,1,2




void IO_Init(void) {
    P0M0 = 0x00; P0M1 = 0x00; // P0口为传统IO口
    P1M0 = 0x03; P1M1 = 0x00; // P1口为传统IO口	P1.1推挽输出  P1.0推挽输出
    P2M0 = 0x00; P2M1 = 0x00; // P2口为传统IO口
    P3M0 = 0x00; P3M1 = 0x00; // P3口为传统IO口
    P4M0 = 0x00; P4M1 = 0x00; // P4口为传统IO口
    P5M0 = 0x00; P5M1 = 0x00; // P5口为传统IO口
    P6M0 = 0x00; P6M1 = 0x00; // P6口为传统IO口
    P7M0 = 0x00; P7M1 = 0x00;	// P7口为传统IO口
    P3PU = 0xFF; //上拉
    P1PU = 0x50; //上拉
}

void MCU_Init(void)
{
    // P0M0 = 0x00; P0M1 = 0xff;
    // P1M0 = 0x00; P1M1 = 0xff;
    // P2M0 = 0x00; P2M1 = 0xff;
    // P3M0 = 0x00; P3M1 = 0xff;

    // 时钟配置
    //	CKSEL		=0;			// 使用内部高精度IRC  
    //	CLKDIV		=1;			// 主时钟分频系数（出来是系统时钟），1-255分频
                                // 下载程序IRC选择11.0592MHz,分频后系统时钟为11.0592MHz
    HIRCCR |= 0X80;		// 开内部IRC晶振
    while (!(HIRCCR && 0x01)); // 等待时钟就绪标志位
    //MCLKOCR=0X01;

// 初始化IO口
    IO_Init();
    //	Power_L		=0;
    // P3PU = 0x4c; //P3.6 P3.3 P3.2 上拉





    // 引脚功能定义
    P_SW1 = 0x80;				// 串口1映射到RX=P1.6 TX=P1.7	ccp0=P3.2  ccp1=P3.3
    P_SW2 = 0x00;				// I2C的SDA-P1.4
    // P_SW2 |= 0X80;			// 读写使能

    // T0和T1配置
    AUXR &= 0x3F;			// T0/T1时钟12分频
    //AUXR		|=0x80;			// T0时钟1T模式=1分频
    //TMOD		=0x20;			// GATE C/T M M0 GATE C/T M M0		(定时器1工作在8位计数方式，定时器0工作在16位计数方式)
    TMOD = 0x00;			// T1,T0工作在16位自动重装载
    TH0 = PWM0_CS_H;
    TL0 = PWM0_CS_L;
    TH1 = Timer_10ms_H;
    TL1 = Timer_10ms_L;
    TR0 = 1;				// 运行T0计数16BIT
    TR1 = 1;				// 运行T1计数16BIT
    ET0 = 1;	 			// T0中断允许
    ET1 = 1;				// T1中断允许


    // 唤醒中断
    IT0 = 1;             // INT0下降沿中断
    IT1 = 1;             // INT1下降沿中断
    EX0 = 1;				// INT0打开中断	p3.2
    EX1 = 1;				// INT1打开中断	p3.3
    INTCLKO = 0X10;			// iNT2打开下降沿中断 p3.6
    INTCLKO |= 0X20;			// iNT3打开下降沿中断 p3.7
    INTCLKO |= 0X40;			// iNT4打开下降沿中断 p3.2
    ES = 1;				// 串口1中断允许(唤醒功能)p1.6
    // IE2 = ES2;
    // IE2 |=ES3;
    // IE2 |=ES4;

    // ES = 1;				// 串口1中断允许(唤醒功能)p1.6


    //	I2CCFG		=0X80;			// 全能I2C模块的从机模式
    //	I2CSLCR		=0X40;			// 全能起始信号中断 (唤醒功能)P1.4

    //低电压检测初始化
        //PCON	&=~LVDF;		//清低电压中断标志
        //RSTCFG	=LVD3V0;		//低于3.0V中断
        //ELVD	=1;				//低电压检测中断便能

    //PWM初始化
    CCON = 0x00;
    CMOD = 0x00;			//PCA时钟为系统时钟/12
    // CMOD	=0x04;			//PCA时钟定时器0溢出脉冲
    CL = 0;
    CH = 0;
    //7位PWM方式
    CCAPM0 = 0x42;			// 允许CCP0比较功能，脉宽调允许

    PCA_PWM0 = 0x43;			// 照明PWM输出灭 (PWM固定输出低）
    CCAP0H = 0XFF;
    CCAP0L = 0XFF;

    //7位PWM方式
    CCAPM1 = 0x42;			// 允许CCP1比较功能，脉宽调允许

    // PCA_PWM1	=0x83;			// 激光瞄PWM灭 (PWM固定输出低）
    // CCAP1H		=0Xff;
    // CCAP1L		=0Xff;

    PCA_PWM1 = 0x43;			// 激光瞄PWM灭 (PWM固定输出低）
    CCAP1H = 0XFF;
    CCAP1L = 0XFF;



    CR = 1;				// 启动PCA计数
    EA = 1;				//总中断允许


    //	PCON	=IDL;			//MCU进入省电模式
    //	PCON	=PD;			//MCU进入掉电模式



}


extern  KEY_T s_KEY[6];



main()
{

    unsigned char led_i;

    MCU_Init();
    //  = STATE_LIGHTING_MOMENTARY;
    read_eeprom();
    // P13 = 0;
    //PCON	=PD;			//MCU进入掉电模式
    //PCON	=IDL;			//MCU进入省电模式
    while (1)
    {
        /*
            按键处理,按下开启，抬起关闭，双击开启与关闭切换
            IN_KEY1 P36     // 开关按键              对应按键码 KEYNUM1; 对应结构体 s_KEY[1];
            IN_KEY2 P33     // 鼠尾按键1,开关功能    对应按键码 KEYNUM2; 对应结构体 s_KEY[2];
            IN_KEY3 P32     // 鼠尾按键2,绿激光优先  对应按键码 KEYNUM3; 对应结构体 s_KEY[3];
            KEY2和KEY3同时按下长按,切换激光瞄PWM输出功率对应按键码 KEYNUM0; 对应结构体 s_KEY[0];

        // */
        if (tcountForSleep > 55)
        {
            State_LED = 0;
            LED_ON(State_LED);
            // P3PU = 0X00;
            // P1PU = 0X00;

            // McuPDSleep();
            PCON = PD;  // MCU进入掉电模式
            tcountForSleep = 0;
            _nop_();    // 掉电模式被唤醒后, MCU首先会进入此语句
            _nop_();    //然后再进入中断服务程序
            _nop_();
            // P3PU = 0xFF; //上拉
            // P1PU = 0x50; //上拉

        }


        KeyNum = bsp_GetKey();

        if (KeyNum)
        {

            switch (KeyNum) // 按键码
            {
            case KEYNUM_0_LONG:
                if (switch_1 == 0xff) {
                    switch_1 = 0x00;
                    write_eeprom();
                }
                else {
                    switch_1 = 0xff;
                    write_eeprom();
                }
                Green_Aim_Handler(1);
                DelayMs(50);
                Green_Aim_Handler(0);
                State_LightingLatched = 0;
                State_GreenLatched = 0;
                break;
            case KEYNUM_1_DOUBLE:
                State_LightingLatched = !State_LightingLatched; // 切换照明常亮状态
                State_GreenLatched = 0; // 绿光常亮状态关闭
                break;
            case KEYNUM_3_DOUBLE:
                State_LightingLatched = !State_LightingLatched; // 切换照明常亮状态
                State_GreenLatched = 0; // 绿光常亮状态关闭
                break;
            case KEYNUM_2_DOUBLE:
                State_GreenLatched = !State_GreenLatched; // 切换绿光常亮状态
                State_LightingLatched = 0; // 照明常亮状态关闭
                break;

            default:
                break;
            }
            if (State_LightingLatched || State_GreenLatched) {
                if ((KeyNum == KEYNUM_0_DOWN) ||
                    (KeyNum == KEYNUM_1_SINGLE) ||
                    (KeyNum == KEYNUM_2_SINGLE) ||
                    (KeyNum == KEYNUM_3_SINGLE))

                {
                    State_LightingLatched = 0;
                    State_GreenLatched = 0;

                }

            }


        }

        if ((CurrentSelectorAim != LastSelectorAim) ||
            (CurrentSelectorIll != LastSelectorIll) ||
            KeyNum != 0)
        {
            // OUT_LED_GREEN = !OUT_LED_GREEN;
            // tcountForSleep = 0;
            LastSelectorAim = CurrentSelectorAim;
            LastSelectorIll = CurrentSelectorIll;
            // LastKey1 = s_KEY[1].State;
            // LastKey2 = s_KEY[2].State;
            // LastKey3 = s_KEY[3].State;

            StateUpdate();
            // LasersUpdate_Handler();


        }





    }

}


